home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DIHDRRD.C < prev    next >
C/C++ Source or Header  |  1995-07-29  |  5KB  |  178 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    dihdrrd.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to read the file header of a data file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Read file header of a data file given the table offset.
  49. //    Parameters:    hpf        Physical file handle
  50. //                        phdr        Buffer to read header into.
  51. //       Returns:    TRUE if successful.
  52. //----------------------------------------------------------------------------
  53. BOOL FN_E DioHeaderRead(HPF hpf, PDATAHDR phdr)
  54. {
  55.     Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
  56.     Assert(di.physical[hpf].fUsed);
  57.     Assert(phdr);
  58.     if (di.physical[hpf].fl & PF_ERROR)
  59.         return FALSE;
  60.  
  61.     if (di.physical[hpf].pdatahdr == NULL)
  62.         {
  63.         di.physical[hpf].pdatahdr = (PDATAHDR)MemAlloc(sizeof(DATAHDR));
  64.         if (di.physical[hpf].pdatahdr == NULL)
  65.             {
  66.             ErrorNoMem();
  67.             goto ERROR_EXIT;
  68.             }
  69.         if (!DioHeaderReadHandle(di.physical[hpf].hf, di.physical[hpf].pdatahdr))
  70.             goto ERROR_EXIT;
  71.         }
  72.     *phdr = *di.physical[hpf].pdatahdr;
  73.     if (di.physical[hpf].fWriteable        // If file is writeable, destroy cache
  74.     && di.physical[hpf].pdatahdr)
  75.         {
  76.         MemFree(di.physical[hpf].pdatahdr);
  77.         di.physical[hpf].pdatahdr = NULL;
  78.         }
  79.     return TRUE;
  80.  
  81. ERROR_EXIT:
  82.     if (di.physical[hpf].pdatahdr)
  83.         {
  84.         MemFree(di.physical[hpf].pdatahdr);
  85.         di.physical[hpf].pdatahdr = NULL;
  86.         }
  87.     di.physical[hpf].fl |= PF_ERROR;
  88.     return FALSE;
  89. }
  90.  
  91.  
  92. //----------------------------------------------------------------------------
  93. //   Description:    Read file header of a data file given the file handle.
  94. //                          This routine exists because at startup, the file has not
  95. //                        been placed in the physical file table.
  96. //    Parameters:    hf        File handle
  97. //                        phdr    Buffer to read header into.
  98. //       Returns:    TRUE if successful.
  99. //----------------------------------------------------------------------------
  100. BOOL FN_E DioHeaderReadHandle(HF hf, PDATAHDR phdr)
  101. {
  102.     FPOS fsize;
  103.  
  104.     //
  105.     //    Go to great extents to verify that this is an actual
  106.     //    and uncorrupted data file. This is the only place that the
  107.     //    header data is verified. Directory entries are verified as
  108.     //    they are read.
  109.     //
  110.     Assert(phdr);
  111.     fsize = FileGetSize(hf);
  112.     if (fsize < sizeof(DATAHDR)
  113.     || (fsize % sizeof(DATAHDR)) != 0)
  114.         {
  115.         Error("Data file size is incorrect (%ld bytes).", fsize);
  116.         return FALSE;
  117.         }
  118.     if (!FileRead(hf, (PBYTE)phdr, sizeof(DATAHDR), 0L))
  119.         return FALSE;
  120.  
  121.     if (phdr->lId != DFH_ID)
  122.         {
  123.         Error("Data file is invalid.\nIncorrect file id.");
  124.         return FALSE;
  125.         }
  126.     if (phdr->usVersion < DFH_VERION)
  127.         {
  128.         Error("Data file is invalid.\nFormat not supported.");
  129.         return FALSE;
  130.         }
  131.     if (phdr->crc != (ULONG)CrcCalc((PBYTE)phdr, sizeof(DATAHDR) - sizeof(ULONG)))
  132.         {
  133.         Error("Data file is corrupt.\nFile header CRC code does not verify.");
  134.         return FALSE;
  135.         }
  136.     return TRUE;                        
  137. }
  138.  
  139.  
  140. //----------------------------------------------------------------------------
  141. //   Description:    Release header cache
  142. //    Parameters:    hpf        Physical file handle
  143. //       Returns:    TRUE if successful.
  144. //----------------------------------------------------------------------------
  145. BOOL FN_E DioHeaderRelease(HPF hpf)
  146. {
  147.     Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);    
  148.     Assert(di.physical[hpf].fUsed);
  149.     if (di.physical[hpf].pdatahdr)
  150.         {
  151.         MemFree(di.physical[hpf].pdatahdr);
  152.         di.physical[hpf].pdatahdr = NULL;
  153.         }
  154.     return TRUE;
  155. }
  156.  
  157.  
  158. //----------------------------------------------------------------------------
  159. //   Description:    Release directory cache
  160. //    Parameters:    
  161. //       Returns:    TRUE if successful.
  162. //----------------------------------------------------------------------------
  163. BOOL FN_E DioHeaderReleaseAll(void)
  164. {
  165.     BOOL fResult = TRUE;
  166.     HPF hpf;
  167.  
  168.     for (hpf = 0; hpf < MAX_PHYSICAL_FILES; ++hpf)
  169.         if (di.physical[hpf].fUsed && !DioHeaderRelease(hpf))
  170.             fResult = FALSE;
  171.  
  172.     return fResult;
  173. }
  174. //----------------------------------------------------------------------------
  175. //------------------------------- End of File --------------------------------
  176. //----------------------------------------------------------------------------
  177.  
  178.